//
//  TabBar.swift
//  DHLive
//
//  Created by Mac on 2021/6/27.
//

import UIKit

enum ItemType: Int {
    case ItemTypeShow = 100
    case ItemTypeMe
    case ItemTypeLaunch
}

protocol TabBarDelegate: NSObjectProtocol {
    func tabbarDelegate(_ clickButton: Int)
}

class TabBar: UITabBar {

    weak var tbDelegate: TabBarDelegate?
    
    lazy var bgView: UIImageView = {
       let bg = UIImageView(image: UIImage(named: "global_tab_bg"))
        bg.sizeToFit()
        return bg
    }()
    
    lazy var dataList: Array<String> = {
       let ds = ["tab_live", "tab_me"]
        return ds
    }()
    
    var lastBtn: UIButton = UIButton()
    
    var centerBtn: UIButton = UIButton()
    override init(frame: CGRect) {
        super.init(frame: frame)
        addSubview(bgView)
        addBtns()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        superview?.layoutSubviews()
        let width = kScreenWidth / CGFloat(dataList.count)
        let height = self.frame.height
        for view in subviews {
            if view is UIButton {
                if view.tag == ItemType.ItemTypeLaunch.rawValue {
                    view.center = CGPoint(x: kScreenWidth*0.5, y: 0)
                }else {
                    view.frame = CGRect(x: CGFloat((view.tag - ItemType.ItemTypeShow.rawValue)) * width, y: 0, width: width, height: height)
                }
            }else {
                view.frame = CGRect(x: 0, y: 0, width: kScreenWidth, height: height)
            }
        }
    }
}


//MARK:- 自定义TabBar添加按钮
extension TabBar {
    func addBtns() {
        do {
            let btn = UIButton(type: .custom)
            btn.adjustsImageWhenHighlighted = false
            btn.setImage(UIImage(named: "tab_live"), for: .normal)
            btn.setImage(UIImage(named: "tab_live"+"_p"), for: .selected)
            btn.tag = ItemType.ItemTypeShow.rawValue
            btn.isSelected = true
            addSubview(btn)
            btn.addTarget(self, action: #selector(tabbarBtnAction(sender:)), for: .touchUpInside)
            
            lastBtn = btn
        }
        
        do {
            let btn = UIButton(type: .custom)
            btn.adjustsImageWhenHighlighted = false
            btn.setImage(UIImage(named: "tab_me"), for: .normal)
            btn.setImage(UIImage(named: "tab_me"+"_p"), for: .selected)
            btn.tag = ItemType.ItemTypeMe.rawValue
            btn.isSelected = false
            addSubview(btn)
            btn.addTarget(self, action: #selector(tabbarBtnAction(sender:)), for: .touchUpInside)
        }
        
        do {
            let btn = UIButton(type: .custom)
            btn.setImage(UIImage(named: "tab_launch"), for: .normal)
            btn.tag = ItemType.ItemTypeLaunch.rawValue
            btn.sizeToFit()
            addSubview(btn)
            btn.addTarget(self, action: #selector(tabbarBtnAction(sender:)), for: .touchUpInside)
            centerBtn = btn
        }
    }
}


//MARK:- 自定义TabBar点击事件
extension TabBar {
    @objc func tabbarBtnAction(sender: UIButton) {
        if tbDelegate != nil {
            tbDelegate?.tabbarDelegate(sender.tag)
        }
        if sender.tag == ItemType.ItemTypeLaunch.rawValue {
            
            return
        }
        lastBtn.isSelected = false
        sender.isSelected = !sender.isSelected
        lastBtn = sender
        
        UIView.animate(withDuration: 0.25) {
            sender.transform = CGAffineTransform(scaleX: 1.2, y: 1.2)
        } completion: { _ in
            UIView.animate(withDuration: 0.25) {
                sender.transform = .identity
            }
        }
    }
}


//MARK:- 自定义TabBar超出父控件
extension TabBar {
    override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
        if centerBtn.frame.contains(point) {
            return true
        }
        return super.point(inside: point, with: event)
    }
}
